<html>
<head><meta charset="utf-8"><title>Optional operands in instructions · project-inline-asm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/index.html">project-inline-asm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html">Optional operands in instructions</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221501218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221501218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221501218">(Jan 04 2021 at 08:57)</a>:</h4>
<p>Hello, I can't seem to find any documentation on how to use optional operands, is this possible in the current syntax? Is it as simple as using <code>Option</code> as the type for the  argument? E.g. If I had something like <code>"foo {0} {1}"</code> I'd like this to be <code>"foo {0}"</code> if <code>{1}</code> is <code>None</code>.</p>



<a name="221501314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221501314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221501314">(Jan 04 2021 at 08:58)</a>:</h4>
<p>This is not possible. You should use an <code>if let</code> and two separate <code>asm!</code> blocks.</p>



<a name="221503253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221503253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221503253">(Jan 04 2021 at 09:24)</a>:</h4>
<p>Hmm that is unfortunate. SPIR-V has a lot of optional operands, for example anything with <a href="https://www.khronos.org/registry/spir-v/specs/1.0/SPIRV.html#_a_id_image_a_image_instructions">sampling images</a> comes with its own set of <a href="https://www.khronos.org/registry/spir-v/specs/1.0/SPIRV.html#Image_Operands">optional operands</a> and the amount of additional operands itself depends on the value of the optional operand. So having to have an <code>if let</code> would lead to a bit of a combinational explosion trying to have every <code>asm!</code> block cover seven different possibilities and all of the potential combinations of those seven.</p>



<a name="221568911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221568911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221568911">(Jan 04 2021 at 19:29)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> Can you clarify and give an example of an optional operand in SPIR-V?</p>



<a name="221569155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221569155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221569155">(Jan 04 2021 at 19:31)</a>:</h4>
<p>Are you saying that there are instructions that can be called with different numbers of operands?</p>



<a name="221569198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221569198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221569198">(Jan 04 2021 at 19:31)</a>:</h4>
<p>Or do they always get called with the same number of operands, but some of those operands can be something akin to None in the encoding?</p>



<a name="221569220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221569220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221569220">(Jan 04 2021 at 19:31)</a>:</h4>
<p>How do the  instructions get encoded?</p>



<a name="221569473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221569473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221569473">(Jan 04 2021 at 19:33)</a>:</h4>
<p>Do other SPIR-V assemblers have a concept of this?</p>



<a name="221569810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221569810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221569810">(Jan 04 2021 at 19:36)</a>:</h4>
<p>If this is a common idiom in SPIR-V assembly, and is commonly used in other assemblers, I think it'd make sense to support it. I don't think we should accept <code>Option&lt;uN&gt;</code> and just substitute an empty string, but I <em>do</em> think it'd be reasonable to have a substitution modifier (like the size modifiers), along the lines of <code>foo {0} {1:opt}</code>.</p>



<a name="221570032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221570032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221570032">(Jan 04 2021 at 19:38)</a>:</h4>
<p>Is there an unambiguous syntax for those instructions that sets the appropriate operand type, in case you have multiple optional operands? It looks like there needs to be a flag indicating the types of operands.</p>



<a name="221623414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623414">(Jan 05 2021 at 08:40)</a>:</h4>
<blockquote>
<p>Are you saying that there are instructions that can be called with different numbers of operands?</p>
</blockquote>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> To my knowledge yes. For example if you look at of the <code>OpImageSampleImplicitLod</code> instruction, they have an optional operand at the end of the instruction, which itself is a bitmask that indicates what <em>additional</em> operands if any are part of the instruction. This bitmask can be combined so you could have a variable amount of operands after an instruction, depending on what the bitmask is. I've pasted some examples from the SPIRV-Tools repo (which contains the official spirv assembler).</p>
<div class="codehilite" data-code-language="spirv"><pre><span></span><code>%res1 = OpImageSampleImplicitLod %f32vec4 %simg %f32vec2_hh
%res2 = OpImageSampleImplicitLod %f32vec4 %simg %f32vec2_hh Bias %f32_0_25
%res4 = OpImageSampleImplicitLod %f32vec4 %simg %f32vec2_hh ConstOffset %s32vec2_01
%res5 = OpImageSampleExplicitLod %f32vec4 %simg %f32vec2_hh Grad|ConstOffset %22 %24 %29
</code></pre></div>
<p>These image instructions are the most extreme, apart from them there are a few instructions that have one or in some cases two optional parameters which are known ahead of time.</p>



<a name="221623480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623480">(Jan 05 2021 at 08:41)</a>:</h4>
<p>Would you expect the assembler to put the bitmask together for you, or would you expect to do that yourself?</p>



<a name="221623563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623563">(Jan 05 2021 at 08:42)</a>:</h4>
<p>I would expect to provide to it.</p>



<a name="221623607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623607">(Jan 05 2021 at 08:43)</a>:</h4>
<p>e.g. Grad=xyz, ConstOffset=abc, which would let you pass the args in any order and the assembler would know what order to provide them to the instruction in.</p>



<a name="221623745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623745">(Jan 05 2021 at 08:45)</a>:</h4>
<p>Oh, there's a set order. From the spec "If there are multiple following operands indicated, they are ordered: Those indicated by smaller-numbered bits appear first. At least one bit must be set". Grad is <code>0x4</code> for example and <code>ConstOffset</code> is <code>0x8</code>.</p>



<a name="221623810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623810">(Jan 05 2021 at 08:46)</a>:</h4>
<p>That's what I figured; that's why I was asking if it would help if the assembler did it for you.</p>



<a name="221623890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623890">(Jan 05 2021 at 08:47)</a>:</h4>
<p>In any case, thinking about it, I don't see how Option helps given the semantic here, unless the instructions are encoded identically.</p>



<a name="221623897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623897">(Jan 05 2021 at 08:47)</a>:</h4>
<p>The assembly string is processed at compile time.</p>



<a name="221623973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623973">(Jan 05 2021 at 08:48)</a>:</h4>
<p>So either you need to know the operands at compile time, or you need an instruction encoding that works whether you have the operands or not.</p>



<a name="221623993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221623993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221623993">(Jan 05 2021 at 08:49)</a>:</h4>
<p>Does that make sense?</p>



<a name="221624023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221624023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221624023">(Jan 05 2021 at 08:49)</a>:</h4>
<p>(I don't know SPIR-V, so I'm trying to work out semantics from what you've said and linked to.)</p>



<a name="221624104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Optional%20operands%20in%20instructions/near/221624104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Optional.20operands.20in.20instructions.html#221624104">(Jan 05 2021 at 08:50)</a>:</h4>
<p>Well I feel like I might be out of my depth to say whether or not it would be helpful if it did it for me. I'd have to defer to someone who knows SPIR-V better than I.</p>
<blockquote>
<p>In any case, thinking about it, I don't see how Option helps given the semantic here, unless the instructions are encoded identically.</p>
</blockquote>
<p>Yeah, I didn't mean to imply it should be <code>Option</code>, that was just what I intuited it might work without seeing mention of optional operands in the documentation.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>